/**
 * Created by MAJIANGTAO on 2015/11/11.
 */
define('session',[],function(){
    var isLogin = false;
    //错误类型
    var errorType = function(data){
        if(typeof data.errorType != 'undefined'){
            //判断session过期
            if(data.errorType == 'SessionExpired'){
                layer.msg('会话过期');
                bugCloseStorage.delItem('token');
                var bugcloseAutoLogin = bugCloseStorage.getItem('bugcloseAutoLogin');
                var email = bugCloseStorage.getItem('username');
                var pwd = bugCloseStorage.getItem('pwd');
                if(isBoolean(bugcloseAutoLogin)){
                    if(!isLogin){
                        isLogin = true;
                        $.ajax({
                            type:'post',
                            dataType:'json',
                            url:GLOBAL.URL+'user/login',
                            data:{email:email,password:pwd},
                            success:function(data){
                                isLogin = false;
                                layer.closeAll();
                                if(data.success){
                                    //window.token = data.root;
                                    bugCloseStorage.setItem('token',data.root);
                                    bugCloseStorage.setItem('username',email);
                                    bugCloseStorage.setItem('pwd',pwd);
                                    if($('#projectInfoMenu').hasClass('active')){
                                        window.location.reload();
                                    }
                                    $('#side-menu li').each(function(index,el){
                                        if($(el).hasClass('active')){
                                            loadPage($('#main'),$(el).attr('data-url'));
                                        }
                                    });
                                }else{
                                    $('#userLogin').modal('show');
                                }
                            },
                            error:function(){
                                isLogin = false;
                                layer.closeAll();
                                $('#userLogin').modal('show');
                            }
                        });
                    }
                }else{
                    $('#userLogin').modal('show');
                }
                
                // layer.msg('会话过期，请重新登录');
                bugCloseStorage.setItem('bugInfoUrl',window.location.href);
                // setTimeout(function(){
                //     bugCloseStorage.delItem('token');
                //     window.location.href='login.html'
                // },1000);
                return false;
            }
            //密码错误
            if(data.errorType == 'PasswordError'){
                layer.closeAll();
                layer.msg(data.errorMessage);
                return false;
            }
            //项目已关闭
            if(data.errorType == 'AccessDenied'){
                layer.closeAll();
                layer.msg(data.errorMessage);
                return false;
            }
            //存在记录
            if(data.errorType == 'RecordExists'){
                layer.closeAll();
                layer.msg(data.errorMessage);
                return false;
            }
            //不存在记录
            if(data.errorType == 'RecordNotExists'){
                layer.closeAll();
                layer.msg(data.errorMessage);
                return false;
            }
            //参数错误
            if(data.errorType == 'ParameterError'){
                layer.closeAll();
                if(typeof data.root != 'undefined'){
                    if(typeof data.root.tagIds != 'undefined'){
                        layer.msg(data.root.tagIds)
                    }else if(typeof data.root.captcha != 'undefined'){
                        layer.msg(data.root.captcha);
                    }else{
                        for(var j in data.root){
                            layer.msg(data.root[j]);
                        }
                        //layer.msg(data.errorMessage);
                    }
                }else{
                    layer.msg(data.errorMessage);
                }
                return false;
            }
            //权限不足
            if(data.errorType == 'RightLimited'){
                layer.closeAll();
                layer.msg(data.errorMessage);
                return false;
            }
            //项目所有者不能退出项目
            if(data.errorType == 'CanNotDelete'){
                layer.closeAll();
                layer.msg(data.errorMessage);
                return false;
            }
            //系统错误
            if(data.errorType == 'SystemError'){
                layer.closeAll();
                layer.msg('操作失败！系统错误');
                return false;
            }
            //超过最大创建项目数
            if(data.errorType == 'FunctionLimited'){
                layer.closeAll();
                layer.msg(data.errorMessage);
                return false;
            }
            //其他情况
            if(data.errorMessage != 'undefined'){
                layer.closeAll();
                layer.msg(data.errorMessage);
            }
        }
    };
    //存储项目成员
    var _projectUsers = '';
    var currProjectUsers = {
        setUsers : function (obj) {
            _projectUsers = obj;
        },
        getUsers : function(){
            return _projectUsers;
        }
    };
    //存储筛选条件
    var _filter = '';
    var bugFilter = {
        setFilter : function (json) {
            _filter = json;
        },
        getFilter : function () {
            return _filter;
        },
        removeFilter : function () {
            _filter = ''
        }
    };
    //当前产品的id，用于版本管理
    var _currProductIdForVersion = '';
    var currProductIdForVersion = {
        setId : function (id) {
            _currProductIdForVersion = id;
        },
        getId : function () {
            return _currProductIdForVersion;
        },
        removeId : function () {
            _currProductIdForVersion = '';
        }
    };

    return {
        errorType : errorType,
        currProjectUsers : currProjectUsers,
        bugFilterParam : bugFilter,
        currProductIdForVersion : currProductIdForVersion
    }
});
